#!/usr/bin/env ruby

# frozen_string_literal: true

$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))

require 'gitlab/orchestrator/cli'

Signal.trap('INT') do
  warn("\n#{caller.join("\n")}: interrupted")
  exit(1)
end

module Orchestrator
  class << self
    def run
      Gitlab::Orchestrator::Helpers::Output.force_color! if force_color?

      Gitlab::Orchestrator::CLI.start
    rescue Gitlab::Orchestrator::CLI::Error => e
      puts "ERROR: #{e.message}"
      exit 1
    end

    private

    def force_color?
      ENV["ORCHESTRATOR_FORCE_COLOR"]&.match?(/^(true|yes|1)$/i) || %w[--force-color --force-color=true].any? do |arg|
        ARGV.include?(arg)
      end
    end
  end
end

Orchestrator.run
